#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'dong'


import requests
from PIL import Image
from pytesser import image_to_string

class fsz(object):

    def __init__(self):
        self.s = requests.session()
        self.headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language":"zh-CN,zh;q=0.8",
            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36",
            "Host": "www.360guakao.com",
            "Referer": "http://www.360guakao.com/fabu.php?cityid=0&catid=36",
            # "Content-Type": "multipart/form-data; charset=UTF-8"
            # "Content-Type": "text/html; charset=gbk"
        }

    def get_verifyCode(self):
        '''
        获取验证码
        :return:
        '''
        url = 'http://www.360guakao.com/randcode.php'
        r = self.s.get(url)
        with open('360.jpeg','wb') as f:
            f.write(r.content)

        im = Image.open('360.jpeg')
        img = im.convert('L')
        code = str(image_to_string(img)).strip()
        print code
        if len(code) != 4:
            print 'len error'
            return self.get_verifyCode()
        else:
            if not code.isdigit():
                print 'int error'
                return self.get_verifyCode()
            else:
                print 'ok'
                return code

    def post_msg(self):
        '''

        :return:
        '''

        url = 'http://www.360guakao.com/member.php'

        data = {
            'mod': 'login',
            'action': 'dopost',
            'url': '',
            'userid': '17727766372',
            'userpwd': 'ab654321',
            'memory': 'on',
            'log_submit': '������¼',
        }

        r = self.s.post(url, data)
        print r.status_code
        print r.headers
        print r.text

        post_url = 'http://www.360guakao.com/fabu.php?action=input'


        checkCode = self.get_verifyCode()

        post_data = {
            "act": (None, 'dopost'),
            "ismember": (None, '1'),
            "catid": (None, '36'),
            "id": (None, ''),
            "mixcode": (None, '5d1c53d4228ab35a0f7b18cb656967a9'),
            "lat": (None, ''),
            "lng": (None, ''),
            # "catid": (None, '36'),
            "cityid": (None, '6'),
            "areaid": (None, '106'),
            "endtime": (None, '0'),
            "title": (None, '急招注册岩土（退休也可）若干名'.decode('utf-8').encode('gbk')),
            "extra[leixing]": (None, '1'),
            "extra[zhuce]": (None, '1'),
            "extra[zhicheng]": (None, '1'),
            "extra[jiage]": (None, '1'),
            "content": (None, u'江苏无锡勘测院，急招注册岩土（退休也可）若干名，初转不限，3年一次付，退休也可，只挂证，不挂章'.encode('gbk')),
            "contact_who": (None, '�'),
            "tel": (None, '17727766372'),
            "qq": (None, '332899008'),
            "email": (None, ''),
            "checkcode": (None, checkCode),
            # '': '36',
        }

        # self.headers["Referer"] = "http://www.360guakao.com/fabu.php?cityid=0&catid=36"

        p = self.s.post(url=post_url, files=post_data, headers=self.headers)
        print p.status_code
        print p.headers
        with open('c.html', 'w') as f:
            f.write(p.content)

        print p.content
        # print p.text


if __name__ == '__main__':
    f = fsz()
    # print f.get_verifyCode()
    f.post_msg()
